import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/button/public_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';


import '../../../module_common/bridge/native_channel_bridge.dart';

class DestroySuccessPage extends StatefulWidget {
  Map<String, String> parseMap;

  DestroySuccessPage({super.key, required this.parseMap});

  @override
  State<DestroySuccessPage> createState() => _DestroySuccessPageState();
}

class _DestroySuccessPageState extends State<DestroySuccessPage> {
  String walletSalt = 'loading···';
  String walletMnemonic = 'loading···';
  String walletPath = 'loading···';

  // bool isLoading = true;

  Widget TipItem(text) {
    return RichText(
      text: TextSpan(
        children: [
          TextSpan(
            text: '•',
            style: TextStyle(
                fontSize: 12.sp,
                color: AppColors.c_868391,
                fontWeight: FontWeight.bold),
          ),
          WidgetSpan(
            child: SizedBox(width: 6.w), // 这里放置了一个SizedBox作为间隔
          ),
          TextSpan(
            text: text,
            style: TextStyle(
                fontSize: 12.sp,
                color: AppColors.c_868391,
                fontWeight: FontWeight.w400,
                height: (16 / 12).w),
          ),
        ],
      ),
    );
  }

  Widget bottomBtn() {
    return Container(
      padding:
          EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom + 10),
      child: PublicButton(
        text: 'Backup Complete',
        onClick: () async {
          Navigator.popUntil(context, (route) => route.settings.name == '/');
          // 关闭页面
          NativeChannelBridge.instance.closeFlutterPage(num: 1);
          // Navigator.of(context).pushAndRemoveUntil(
          //     MaterialPageRoute(builder: (context) => WalletPage()),
          //     (route) => false);
        },
      ),
    );
  }

  Widget PhraseItem({String title = '', String text = '', Widget? right}) {
    return Container(
      padding: EdgeInsets.all(15.w),
      decoration: BoxDecoration(
          border: Border.all(
            width: 1.w,
            color: AppColors.c_E0E0E0,
          ),
          borderRadius: BorderRadius.all(Radius.circular(10.w))),
      child: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: TextStyle(
                      color: AppColors.c_868391,
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w500),
                ),
                SizedBox(
                  height: 6.w,
                ),
                Text(
                  text,
                  style: TextStyle(
                      color: AppColors.c_333333,
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w500),
                ),
              ],
            ),
          ),
          SizedBox(
            width: 25.w,
          ),
          if (right != null) right

          // SizedBox(
          //   width: 18,
          // ),
        ],
      ),
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      walletSalt = widget.parseMap['salt'] as String;
      walletMnemonic = widget.parseMap['mnemonic'] as String;
      walletPath = widget.parseMap['path'] as String;
    });
    // Future.delayed(Duration(seconds: 2), () {
    //   getParse();
    // });
  }

  // List<T> removeDuplicates<T>(List<T> list) {
  //   Set<T> uniqueSet = Set<T>.from(list);
  //   List<T> uniqueList = uniqueSet.toList();
  //   return uniqueList;
  // }

// 解析助记词显示
  // void getParse() async {
  //   late String pieceA;
  //   late String pieceB;
  //   bool needCheck = true;
  //   List<String> keyList = WalletCheck.getkeyList();
  //   if (keyList.length >= 2) {
  //     pieceA = keyList[0];
  //     pieceB = keyList[1];
  //   } else {
  //     Map<String, String?> walletKeyMap = WalletCheck().walletKeyMap;
  //     if (walletKeyMap.isEmpty) {
  //       WalletCheckStatus walletCheckStatus =
  //           await WalletCheck().checkWalletException(checkUpload: false);
  //       walletKeyMap = walletCheckStatus.keyMap;
  //     }
  //     List list = walletKeyMap.values.toList();
  //     List mylist = list.where((e) => e != null && e != '').toList();
  //     mylist = removeDuplicates(mylist);
  //     if (mylist.length >= 2) {
  //       needCheck = true;
  //       pieceA = mylist[0];
  //       pieceB = mylist[1];
  //     } else {
  //       needCheck = false;
  //     }
  //   }
  //   String? salt = SP().getRandomSalt();
  //   // 存在两片以上
  //   if (needCheck) {
  //     escapeResp res = Mpc().mpcEscape(pieceA, pieceB, salt!);
  //     print('mnemonic===${res.mnemonic}');
  //     print('path===${res.path}');
  //     setState(() {
  //       walletSalt = salt;
  //       walletMnemonic = res.mnemonic;
  //       walletPath = res.path;
  //       isLoading = false;
  //     });
  //   } else {
  //     setState(() {
  //       walletSalt = salt!;
  //       walletMnemonic = '';
  //       walletPath = '';
  //       isLoading = false;
  //     });
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        bottomNavigationBar: bottomBtn(),
        body: Container(
          child: SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  width: double.infinity,
                  decoration: BoxDecoration(
                    color: Colors.cyan,
                    image: DecorationImage(
                        fit: BoxFit.cover,
                        image: AssetImage(
                          'assets/images/wallet/success-bg.png',
                        )),
                  ),
                  padding:
                      EdgeInsets.only(right: 20.w, left: 20.w, bottom: 20.w),
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                          height: MediaQuery.of(context).padding.top,
                        ),
                        SizedBox(
                          height: 20.w,
                        ),
                        Row(
                          children: [
                            Image.asset(
                              'assets/images/wallet/ok.png',
                              width: 20.w,
                              height: 20.w,
                            ),
                            SizedBox(
                              width: 25.w,
                            ),
                            Text(
                              'Destruction successful',
                              style: TextStyle(
                                  fontSize: 20.sp,
                                  fontWeight: FontWeight.w500,
                                  color: AppColors.c_333333),
                            )
                          ],
                        ),
                        SizedBox(
                          height: 9.w,
                        ),
                        Text(
                          'Please securely save your recovery phrase and record them in order.',
                          style: TextStyle(
                              fontSize: 14.sp,
                              fontWeight: FontWeight.w400,
                              color: AppColors.c_333333),
                        ),
                        SizedBox(
                          height: 10.w,
                        ),
                        TipItem(
                            'Once you leave this page, your recovery phrase will no longer be displayed.'),
                        SizedBox(
                          height: 10.w,
                        ),
                        TipItem(
                            'Without a backup, you risk losing access to your wallet assets.'),
                        SizedBox(
                          height: 20.w,
                        ),
                      ]),
                ),
                Transform.translate(
                  offset: Offset(0, -30.w),
                  child: Container(
                    padding: EdgeInsets.all(20.w),
                    decoration: BoxDecoration(
                        color: (walletMnemonic.isNotEmpty &&
                                walletMnemonic.isNotEmpty &&
                                walletPath.isNotEmpty)
                            ? AppColors.c_white
                            : Colors.transparent,
                        borderRadius:
                            BorderRadius.vertical(top: Radius.circular(30))),
                    child: Column(children: [
                      Visibility(
                          visible: walletMnemonic.isNotEmpty,
                          child: Column(
                            children: [
                              PhraseItem(
                                title: 'Recovery Phrase',
                                text: walletMnemonic,
                                right: Image.asset(
                                  'assets/images/wallet/delete_1.png',
                                  width: 43.w,
                                  height: 43.w,
                                ),
                              ),
                              SizedBox(
                                height: 10.w,
                              ),
                            ],
                          )),
                      Visibility(
                          visible: walletMnemonic.isNotEmpty,
                          child: Column(
                            children: [
                              PhraseItem(
                                title: 'Passphrase',
                                text: walletSalt,
                                right: Image.asset(
                                  'assets/images/wallet/delete_2.png',
                                  width: 43.w,
                                  height: 43.w,
                                ),
                              ),
                              SizedBox(
                                height: 10.w,
                              ),
                            ],
                          )),
                      Visibility(
                          visible: walletPath.isNotEmpty,
                          child: Column(
                            children: [
                              PhraseItem(
                                title: 'Path',
                                text: walletPath,
                                right: Image.asset(
                                  'assets/images/wallet/delete_3.png',
                                  width: 43.w,
                                  height: 43.w,
                                ),
                              ),
                            ],
                          )),
                    ]),
                  ),
                )
              ],
            ),
          ),
        ));
  }
}
